package com.java.second_goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.second_goods.entity.Address;
import com.java.second_goods.mapper.AddressMapper;
import com.java.second_goods.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 地址 服务层实现
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    /**
     * 查询地址信息
     *
     * @param id 地址ID
     * @return 地址信息
     */
    @Override
    public Address selectAddressById(Integer id) {
        return addressMapper.selectById(id);
    }

    /**
     * 查询地址列表
     *
     * @param address 地址信息
     * @return 地址集合
     */
    @Override
    public List<Address> selectAddressList(Address address) {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        // 构建查询条件
        if (address.getUserId() != null) {
            queryWrapper.eq(Address::getUserId, address.getUserId());
        }
        if (address.getIsDefault() != null) {
            queryWrapper.eq(Address::getIsDefault, address.getIsDefault());
        }
        return addressMapper.selectList(queryWrapper);
    }

    /**
     * 根据用户ID查询地址列表
     *
     * @param userId 用户ID
     * @return 地址列表
     */
    @Override
    public List<Address> selectAddressByUserId(Integer userId) {
        return addressMapper.selectAddressByUserId(userId);
    }

    /**
     * 查询用户默认地址
     *
     * @param userId 用户ID
     * @return 默认地址
     */
    @Override
    public Address selectDefaultAddressByUserId(Integer userId) {
        return addressMapper.selectDefaultAddressByUserId(userId);
    }

    /**
     * 新增地址
     *
     * @param address 地址信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAddress(Address address) {
        // 如果设置为默认地址，则将用户其他地址设为非默认
        if (address.getIsDefault() != null && address.getIsDefault()) {
            addressMapper.clearDefaultAddress(address.getUserId());
        }
        return addressMapper.insert(address);
    }

    /**
     * 修改地址
     *
     * @param address 地址信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateAddress(Address address) {
        // 如果设置为默认地址，则将用户其他地址设为非默认
        if (address.getIsDefault() != null && address.getIsDefault()) {
            addressMapper.clearDefaultAddress(address.getUserId());
        }
        return addressMapper.updateById(address);
    }

    /**
     * 删除地址信息
     *
     * @param id 地址ID
     * @return 结果
     */
    @Override
    public int deleteAddressById(Integer id) {
        return addressMapper.deleteById(id);
    }

    /**
     * 批量删除地址信息
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAddressByIds(Integer[] ids) {
        return addressMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 设置默认地址
     *
     * @param addressId 地址ID
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int setDefaultAddress(Integer addressId, Integer userId) {
        // 先清除所有默认标记
        addressMapper.clearDefaultAddress(userId);
        // 设置新的默认地址
        return addressMapper.setDefaultAddress(addressId, userId);
    }
}